#Region " Imports "

Imports BlazeApps.Library.Events
Imports BA.Dates
Imports System.Data

#End Region

Partial Class Usercontrols_Event_AddEvent
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Dim DateValues As RecurrenceValues = Nothing

    Protected Property EventGuid() As Guid
        Get
            Return GetGuidFromVS("EventID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("EventID", value)
        End Set
    End Property

    Protected Property OccurenceGuid() As Guid
        Get
            Return GetGuidFromVS("OccurenceID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("OccurenceID", value)
        End Set
    End Property

    Protected Property NewButtonClicked() As Boolean
        Get
            Return GetBooleanFromVS("NewButtonClicked")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("NewButtonClicked", value)
        End Set
    End Property

    Private Enum RecurrencePattern
        Daily = 0
        Weekly = 1
        Montly = 2
        Yearly = 3
    End Enum

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Create Event")

        If IsUserAuthenticated And (EnableUserEvents OrElse IsEventModerator()) Then
            GetQueryStringValues()
            ConfigBaseEditorCtrls()

            If Me.Page.IsPostBack = False Then

				BuildFCKEditor(EditorPageType.SingleEditor)
                LoadPage()

                If Not (EventGuid = Guid.Empty) Then
                    LoadEvent()
                End If
            End If

            LoadSettings()

            Me.uxMemberEventPanel.Visible = ShowControls(SecurityMask.Member, True)

        Else
            Me.uxMemberEventPanel.Visible = False
            Me.uxMessagesLabel.Text = Resources.Resource.AdminDisabledEvents.ToString
        End If

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveEvent()
        If Me.uxRecurringEventCheckBox.Checked Then

            CalculateRecurringDates()

            Dim NewDates As DateTime() = New DateTime(DateValues.Values.Count - 1) {}
            Dim counter As Integer = 0
            Dim RecurrenceID As Guid = Guid.NewGuid

            For Each DateItem As DateTime In DateValues.Values
                NewDates(counter) = DateItem

                Dim RecurrenceItem As EventDetail = EventDetail.NewEventDetail()

                RecurrenceItem.EventTitle = Me.uxEventTitleTextBox.Text.ToString.Trim
				RecurrenceItem.EventDetails = Me.uxEventBodyTextbox.Value.ToString.Trim
				RecurrenceItem.EventDateString = DateItem.ToShortDateString
				RecurrenceItem.Hr = GetIntegerFromSelectedItem(Me.uxHourDropDownList)
				RecurrenceItem.Min = GetIntegerFromSelectedItem(Me.uxMinuteDropDownList)
				RecurrenceItem.Meridian = GetStringFromSelectedItem(Me.uxMeridianDropdownlist)
				RecurrenceItem.UserID = CurrentUserID
				RecurrenceItem.Occurenceid = RecurrenceID

				RecurrenceItem.Save()

				EventGuid = RecurrenceItem.EventID

				uxOptions.ItemGuid = RecurrenceItem.EventID
				uxOptions.SetInitialOptions = True
				uxOptions.SaveOptions()

				counter += 1
			Next
		Else
			Dim Item As EventDetail = EventDetail.NewEventDetail()

			Item.EventTitle = Me.uxEventTitleTextBox.Text.ToString.Trim
			Item.EventDetails = Me.uxEventBodyTextbox.Value.ToString.Trim
			Item.EventDateString = uxEventDateTextBox.Text.ToString.Trim
			Item.Hr = GetIntegerFromSelectedItem(Me.uxHourDropDownList)
			Item.Min = GetIntegerFromSelectedItem(Me.uxMinuteDropDownList)
			Item.Meridian = GetStringFromSelectedItem(Me.uxMeridianDropdownlist)
			Item.UserID = CurrentUserID

			Item.Save()

			EventGuid = Item.EventID

			uxOptions.ItemGuid = Item.EventID
			uxOptions.SetInitialOptions = True
			uxOptions.SaveOptions()
			uxOptions.LoadOptions()

			LoadSettings()
		End If

	End Sub

	Private Sub UpdateEvent()
		Dim Item As EventDetail = EventDetail.GetEventDetail(EventGuid)

		If Me.uxUpdateRecurrenceCheckbox.Checked Then
			Item.Occurenceid = OccurenceGuid
			Item.UpdateOccurence = True
		End If

		Item.EventTitle = Me.uxEventTitleTextBox.Text.ToString.Trim
		Item.EventDetails = Me.uxEventBodyTextbox.Value.ToString.Trim
		Item.EventDateString = uxEventDateTextBox.Text.ToString.Trim
		Item.Hr = GetIntegerFromSelectedItem(Me.uxHourDropDownList)
		Item.Min = GetIntegerFromSelectedItem(Me.uxMinuteDropDownList)
		Item.Meridian = GetStringFromSelectedItem(Me.uxMeridianDropdownlist)

		Item.Save()

	End Sub

#End Region

#Region " Load Data "

	Private Sub LoadPage()
		LoadHourList(Me.uxHourDropDownList)
		LoadMinuteList(Me.uxMinuteDropDownList)
		LoadMeridianList(Me.uxMeridianDropdownlist)
		LoadLongMonthList(Me.uxYearlyMonthDropDownList)
		LoadLongMonthList(Me.uxYearlyRangeMonthDropdownlist)

		If Me.Page.IsPostBack = False Then
			LoadDayList(uxYearlyMonthDayDropDownList)

			Me.uxMonthlyDayTextBox.Text = DateTime.Now.Day
		End If
	End Sub

	Private Sub LoadSettings()
		If (EventGuid = Guid.Empty) Then
			Me.uxOptionsTabPanels.Enabled = False
			Me.uxMyGalleryTabPanel.Enabled = False
			Me.uxAttachmentsTabPanel.Enabled = False
		Else
			Me.uxOptionsTabPanels.Enabled = True
			uxMyGalleryTabPanel.Enabled = (EnableUserGalleries OrElse IsAdmin())
			uxAttachmentsTabPanel.Enabled = (EnableUserGalleries OrElse IsAdmin())
		End If

		Me.uxMyGallery.IsLoadedAsOption = True

	End Sub

	Private Sub LoadEvent()

		Dim Item As EventDetail = EventDetail.GetEventDetail(EventGuid)

		Me.uxEventTitleTextBox.Text = Item.EventTitle
		Me.uxEventBodyTextbox.Value = Item.EventDetails
		Me.uxEventDateTextBox.Text = Item.EventDate.ToShortDateString
		Me.uxHourDropDownList.SelectedValue = Item.Hr.ToString.Trim
		Me.uxMinuteDropDownList.SelectedValue = Item.Min.ToString.Trim
		Me.uxMeridianDropdownlist.SelectedValue = Item.Meridian.Trim
		Me.OccurenceGuid = ConvertToGuid(Item.Occurenceid.ToString)

		If Me.OccurenceGuid = Guid.Empty Then
			Me.uxDeleteRecurrenceCheckbox.Enabled = False
			Me.uxUpdateRecurrenceCheckbox.Enabled = False
		End If

	End Sub

#End Region

#Region " Form Controls Events "

	Protected Sub uxNewEventButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxNewEventButton.Click
		'Response.Redirect(String.Format("{0}.aspx?pid=eve_addevent", AppPage))

		NewButtonClicked = True

		EventGuid = Guid.Empty
		LoadSettings()

		Me.uxEventTitleTextBox.Text = String.Empty
		Me.uxEventBodyTextbox.Value = String.Empty
		Me.uxEventDateTextBox.Text = String.Empty
		Me.uxHourDropDownList.SelectedValue = DropDownDefaultValue
		Me.uxMinuteDropDownList.SelectedValue = DropDownDefaultValue
		Me.uxMeridianDropdownlist.SelectedValue = DropDownDefaultValue

	End Sub

	Protected Sub uxSubmitEventButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSubmitEventButton.Click
		If EventGuid = Guid.Empty Then
			SaveEvent()
		Else
			UpdateEvent()
		End If
	End Sub

	Protected Sub uxDeleteEventsButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxDeleteEventsButton.Click

		If Me.uxDeleteRecurrenceCheckbox.Checked Then
			Dim List As New EventList()
			Dim EventIDs As New DataSet
			Dim DR As DataRow

			EventIDs = List.GetRecurringEvents(OccurenceGuid)

			For Each DR In EventIDs.Tables(0).Rows
				EventDetail.DeleteEventDetail(ConvertToGuid(DR("eventID").ToString()))
			Next

		Else
			EventDetail.DeleteEventDetail(EventGuid)
		End If

		Response.Redirect(String.Format("{0}.aspx?pid=eve_events", AppPage))
	End Sub

	Protected Sub uxResetEventButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxResetEventButton.Click
		If Not (EventGuid = Guid.Empty) Then
			LoadEvent()
		Else
			Me.uxEventTitleTextBox.Text = String.Empty
			Me.uxEventBodyTextbox.Value = String.Empty
			Me.uxEventDateTextBox.Text = String.Empty
			Me.uxHourDropDownList.SelectedValue = DropDownDefaultValue
			Me.uxMinuteDropDownList.SelectedValue = DropDownDefaultValue
			Me.uxMeridianDropdownlist.SelectedValue = DropDownDefaultValue
		End If

		LoadSettings()
	End Sub

	Protected Sub uxYearlyMonthDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxYearlyMonthDropDownList.SelectedIndexChanged

		Me.uxYearlyMonthDayDropDownList.Items.Clear()

		LoadDayList(Me.uxYearlyMonthDayDropDownList, Convert.ToInt32(Me.uxYearlyMonthDropDownList.SelectedIndex) + 1)

	End Sub

	Protected Sub uxRecurrencePatternRadioButtonList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxRecurrencePatternRadioButtonList.SelectedIndexChanged
		If Me.uxRecurrencePatternRadioButtonList.SelectedValue = RecurrencePattern.Daily Then
			Me.uxDailyPanel.Visible = True
			Me.uxWeeklyPanel.Visible = False
			Me.uxMontlyPanel.Visible = False
			Me.uxYearlyPanel.Visible = False
		End If
		If Me.uxRecurrencePatternRadioButtonList.SelectedValue = RecurrencePattern.Weekly Then
			Me.uxDailyPanel.Visible = False
			Me.uxWeeklyPanel.Visible = True
			Me.uxMontlyPanel.Visible = False
			Me.uxYearlyPanel.Visible = False
		End If
		If Me.uxRecurrencePatternRadioButtonList.SelectedValue = RecurrencePattern.Montly Then
			Me.uxDailyPanel.Visible = False
			Me.uxWeeklyPanel.Visible = False
			Me.uxMontlyPanel.Visible = True
			Me.uxYearlyPanel.Visible = False
		End If
		If Me.uxRecurrencePatternRadioButtonList.SelectedValue = RecurrencePattern.Yearly Then
			Me.uxDailyPanel.Visible = False
			Me.uxWeeklyPanel.Visible = False
			Me.uxMontlyPanel.Visible = False
			Me.uxYearlyPanel.Visible = True
		End If
	End Sub

	Protected Sub uxCalculateDatesButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxCalculateDatesButton.Click
		CalculateRecurringDates()
	End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        If NewButtonClicked = False Then
            EventGuid = GetGuidFromQueryString("id")
        End If
    End Sub

    Private Sub ConfigBaseEditorCtrls()
		Me.FCKEditor = Me.uxEventBodyTextbox
    End Sub

    Private Sub CalculateRecurringDates()
        Dim StartDate As DateTime
        Dim EndDate As DateTime

        If Not String.IsNullOrEmpty(Me.uxEventDateTextBox.Text.Trim) Then
            StartDate = Convert.ToDateTime(Me.uxEventDateTextBox.Text.Trim)
        End If

        If Not String.IsNullOrEmpty(Me.uxEndDateTextBox.Text.Trim) Then
            EndDate = Convert.ToDateTime(Me.uxEndDateTextBox.Text.Trim)
        End If

        If Me.uxRecurrencePatternRadioButtonList.SelectedValue = RecurrencePattern.Daily Then
            Dim Daily As DailyRecurrenceSettings

            If Me.uxEndAfterRadioButton.Checked Then
                Daily = New DailyRecurrenceSettings(StartDate, Convert.ToInt32(Me.uxEndAfterNumberTextBox.Text.Trim))
            Else
                Daily = New DailyRecurrenceSettings(StartDate, Convert.ToDateTime(Me.uxEndDateTextBox.Text.Trim))
            End If

            If Me.uxEveryNDayRadioButton.Checked Then
                DateValues = Daily.GetValues(Integer.Parse(Me.uxEveryNDayTextBox.Text.Trim))
            Else
                DateValues = Daily.GetValues(1, DailyRegenType.OnEveryWeekday)
            End If
        End If

        If Me.uxRecurrencePatternRadioButtonList.SelectedValue = RecurrencePattern.Weekly Then
            Dim Weekly As WeeklyRecurrenceSettings
            Dim selectedValues As New SelectedDayOfWeekValues()

            If Me.uxEndAfterRadioButton.Checked Then
                Weekly = New WeeklyRecurrenceSettings(StartDate, Convert.ToInt32(Me.uxEndAfterNumberTextBox.Text.Trim))
            Else
                Weekly = New WeeklyRecurrenceSettings(StartDate, Convert.ToDateTime(Me.uxEndDateTextBox.Text.Trim))
            End If

            selectedValues.Sunday = uxSundayCheckBox.Checked
            selectedValues.Monday = uxMondayCheckBox.Checked
            selectedValues.Tuesday = uxTuesdayCheckBox.Checked
            selectedValues.Wednesday = uxWednesdayCheckBox.Checked
            selectedValues.Thursday = uxThursdayCheckBox.Checked
            selectedValues.Friday = uxFridayCheckBox.Checked
            selectedValues.Saturday = uxSaturdayCheckBox.Checked

            DateValues = Weekly.GetValues(Integer.Parse(uxEveryNWeekTextBox.Text), selectedValues)
        End If

        If Me.uxRecurrencePatternRadioButtonList.SelectedValue = RecurrencePattern.Montly Then
            Dim Monthly As MonthlyRecurrenceSettings

            If Me.uxEndAfterRadioButton.Checked Then
                Monthly = New MonthlyRecurrenceSettings(StartDate, Convert.ToInt32(Me.uxEndAfterNumberTextBox.Text.Trim))
            Else
                Monthly = New MonthlyRecurrenceSettings(StartDate, Convert.ToDateTime(Me.uxEndDateTextBox.Text.Trim))
            End If

            If uxMontlyDayRadioButton.Checked Then
                DateValues = Monthly.GetValues(Integer.Parse(uxMonthlyDayTextBox.Text), Convert.ToInt32(uxMonthlyMonthsTextBox.Text))
            Else
                Monthly.AdjustmentValue = 0
                DateValues = Monthly.GetValues(DirectCast(uxMontlyRangePeriodDropDownList.SelectedIndex, MonthlySpecificDatePartOne), DirectCast(uxMontlyRangeDailyDropDownList.SelectedIndex, MonthlySpecificDatePartTwo), Integer.Parse(uxMontlyRangeNMonthTextBox.Text))
            End If
        End If

        If Me.uxRecurrencePatternRadioButtonList.SelectedValue = RecurrencePattern.Yearly Then
            Dim Yearly As YearlyRecurrenceSettings

            If Me.uxEndAfterRadioButton.Checked Then
                Yearly = New YearlyRecurrenceSettings(StartDate, Convert.ToInt32(Me.uxEndAfterNumberTextBox.Text.Trim))
            Else
                Yearly = New YearlyRecurrenceSettings(StartDate, Convert.ToDateTime(Me.uxEndDateTextBox.Text.Trim))
            End If

            If uxYearlyDayRadioButton.Checked Then
                DateValues = Yearly.GetValues(Integer.Parse(uxYearlyMonthDayDropDownList.SelectedValue), uxYearlyMonthDropDownList.SelectedIndex + 1)
            Else
                Yearly.AdjustmentValue = 0
                DateValues = Yearly.GetValues(DirectCast(uxYearlyRangePeriodDropDownList.SelectedIndex, YearlySpecificDatePartOne), DirectCast(uxYearlyRangeDailyDropDownList.SelectedIndex, YearlySpecificDatePartTwo), DirectCast(uxYearlyRangeMonthDropdownlist.SelectedIndex, YearlySpecificDatePartThree))
            End If
        End If

        Me.uxDatesListBox.Items.Clear()
        Dim NewDates As DateTime() = New DateTime(DateValues.Values.Count - 1) {}
        Dim counter As Integer = 0

        For Each DateItem As DateTime In DateValues.Values
            NewDates(counter) = DateItem
            uxDatesListBox.Items.Add(New ListItem(DateItem.ToShortDateString))
            counter += 1
        Next
    End Sub

#End Region

End Class
